/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.berlin.jsf.validation;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.apache.log4j.Logger;

/**
 *
 * @author Caesar
 */
public class DayValidator implements Validator {

    private static final DateFormat DAY_FORMAT = new SimpleDateFormat("dd.MM.yyyy");

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value == null) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Null is not legal here",
                    "Null is not legal here"));
        }
        if (value.toString().equals("")) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Empty string is not legal here",
                    "Empty string is not legal here"));
        }
        try {
            DAY_FORMAT.parse(value.toString());
        } catch (ParseException ex) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Date format required: dd.MM.yyyy",
                    "Date format required. Get: " + value));
        }
    }

}
